import { createAction } from 'redux-actions';
import * as types from '../constants/ActionTypes';
import * as Api from '../utils/WebAPIUtils.js';
import Tool from '../utils/ToolUtils.js';
import FileUploadTool from '../utils/FileUploadAPIUtils.js';

const getChildrenDeviceAction = createAction(types.FETCH_CHILDREN_DEVICE, data => data);
const saveMsg = createAction(types.SAVE_ERROR_MSG, data => data);
const batchAddAction = createAction(types.BATCH_ADD, data => data);
const fetchOneCategory = createAction(types.GET_ONE_CATEGORY, data => data);
exports.addOne = createAction(types.ADD_ONE, data => data);
exports.editDevice = createAction(types.EDIT_DEVICE, data => data);
exports.fetchChildrenDeviceList = (data) => (dispath) =>
  Tool.post(Api.childrenDevice.query, data, (res) => {
    dispath(getChildrenDeviceAction(res));
  });

exports.uploadFile = (data) => (dispath) =>
  FileUploadTool.post(Api.childrenDevice.uploadFile, data, (res) => {
    dispath(batchAddAction(res.data));
  });

exports.getOneCategory = (data, router) => (dispath) =>
  Tool.post(Api.testCategory.getOne, data, (res) => {
    if (res.code === 200) {
      dispath(fetchOneCategory(res.data));
    } else if (res.code === 90001) {
      console.log('asdfasd');
    }
  }, (error) => { console.log('error'); });
// 新增测试包信息
// ==================
exports.saveChildrenDevice = (data, router) => (dispath) =>
  Tool.post(Api.childrenDevice.save, data, (res) => {
    if (res.code === 200) {
      dispath(saveMsg(res));
      router.push('/testCategory');
      // Tool.post(Api.childrenDevice.query, { id: data.id }, (result) => {
      //   dispath(getChildrenDeviceAction(result));
      // });
    } else if (res.code === 90001) {
      console.log('asdfasd');
    }
  }, (error) => { console.log('error'); });



